package com.huowenjie.controller;

import com.alibaba.fastjson.JSON;
import com.huowenjie.dao.UserDao;
import com.huowenjie.entity.Dept;
import com.huowenjie.entity.User;
import com.huowenjie.service.DeptService;
import com.huowenjie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    DeptService deptService;

    @RequestMapping("userlist")
    public Object userlist(User user,
                           @RequestParam(name = "pageNum",defaultValue = "0")Integer pageNum,
                           @RequestParam(name = "pageSize",defaultValue = "2")Integer pageSize){
        Page<User> userlist = userService.userlist(user, pageNum-1, pageSize);
        System.out.println(userlist);
        return userlist;
    }

    @RequestMapping("dell")
    public boolean dell(String[] ids){
        userService.dell(ids);
        return true;
    }

    @RequestMapping("deptList")
    public Object deptList(){
        List<Dept> depts = deptService.deptList();
        return depts;
    }

    @RequestMapping("add")
    public boolean add(User user){
        try {
            System.out.println(user);
            userService.add(user);
            return true;
        }catch (Exception e){
            return  false;
        }
    }


    @RequestMapping("look")
    public User look(Integer id){
        User look = userService.look(id);
        return look;
    }

}
